import React, {Component} from 'react';
import {createAppContainer} from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';
import LoginPage from './pages/login';
import MainBar from './pages/main';
import NowDataPage from './pages/nowData';
import DrawingPage from './pages/drawing';
import AboutPage from './pages/about';
import HistoryPage from './pages/history';
import ReportPage from './pages/report';
import Storage from 'react-native-storage';
import VideoPage from './pages/video';
import DevicePage from './pages/device';
import {AsyncStorage} from 'react-native';
import FuelRatioPage from './pages/fuelRatio';
import CoalInject from './pages/coalInjectionTrend';

var storage = new Storage({
  // 最大容量，默认值1000条数据循环存储
  size: 1000,
  // 存储引擎：对于RN使用AsyncStorage，对于web使用window.localStorage
  // 如果不指定则数据只会保存在内存中，重启后即丢失
  storageBackend: AsyncStorage,

  // 数据过期时间，默认一整天（1000 * 3600 * 24 毫秒），设为null则永不过期
  defaultExpires: 1000 * 3600 * 22,
  // defaultExpires: 1000 * 3600 * 24,
  // 读写时在内存中缓存数据。默认启用。
  enableCache: true,
});

// 最好在全局范围内创建一个（且只有一个）storage实例，方便直接调用
global.storage = storage;
//全局范围内创建一个车间信息
global.nowDevice = {name: ''};
global.deviceList = null;
const AppNavigator = createStackNavigator(
  {
    aboud: {
      screen: AboutPage,
    },
    nowData: {
      screen: NowDataPage,
      navigationOptions: {
        title: '实时数据',
      },
    },
    drawing: {
      screen: DrawingPage,
      navigationOptions: {
        title: '主要参数画面',//工艺图==>主要参数画面
      },
    },
    video: {
      screen: VideoPage,
      navigationOptions: {
        title: '炉顶热像仪',//视频===>炉顶热像仪
      },
    },
    report: {
      screen: ReportPage,
      navigationOptions: {
        title: '统计报表',
      },
    },
    history: {
      screen: HistoryPage,
      navigationOptions: {
        title: '压量关系趋势',//历史数据  ==>压量关系趋势
      },
    },
    device: {
      screen: DevicePage,
      navigationOptions: {
        title: '设备在线',
      },
    },
    main: {
      screen: MainBar,
      navigationOptions: {
        title: '首页',
        header: null,
      },
    },
    coalinject: {
      screen: CoalInject,
      navigationOptions: {
        title: '智能温控趋势',//喷煤趋势==>智能温控趋势
      },
    },
    fuelratio: {
      screen: FuelRatioPage,
      navigationOptions: {
        title: '燃料比趋势',
      },
    },
    login: {
      screen: LoginPage,
      navigationOptions: {
        title: '登录',
        headerTitleStyle: {
          alignSelf: 'center',
          textAlign: 'center',
          flex: 1,
        },
      },
    },
  },
  {
    initialRouteName: 'login',
    //默认路由，就是第一个要显示的页面
  },
);

// export default createAppContainer(AppNavigator);

const AppContainer = createAppContainer(AppNavigator);

function getActiveRouteName(navigationState) {
  if (!navigationState) {
    return null;
  }
  const route = navigationState.routes[navigationState.index];
  if (route.routes) {
    return getActiveRouteName(route);
  }
  return route.routeName;
}

export default () => (
  <AppContainer
    onNavigationStateChange={(prevState, currentState, action) => {
      const currentScreen = getActiveRouteName(currentState);
      // const prevScreen = getActiveRouteName(prevState);
      global.currentScreen = currentScreen;
    }}
  />
);
